<?php
/**
 * 分页类
 * @author Administrator
 *
 */
class Page {
	public $countPage; // 共几页
	public $which; // 从第几条开始
	public $whichPage; // 当前第几页
	public $several; // 几条分页
	public $count; // 共几条
	public $page_par='page';//网页参数
	/**
	 * 分页类构造函数
	 * 
	 * @param unknown_type $count
	 *        	共几条
	 * @param unknown_type $several
	 *        	几条分页
	 * @param unknown_type $whichPage
	 *        	第几页
	 */
	public function __construct($count, $several,$page_par='page') {
		if(isset($page_par)){
			$this->page_par = $page_par;
		}
		$whichPage = isset($_GET[$this->page_par]) ? $_GET[$this->page_par]:1;
		if(!preg_match("/^[0-9]+$/",$whichPage)){
			$whichPage=1;
		}
		if ($whichPage <= 0) {
			$whichPage = 1;
		}
		$this->countPage = intval ( ($count + ($several - 1)) / $several );
		$this->which = ($whichPage - 1) * $several;
		$this->whichPage = $whichPage;
		$this->several = $several;
		$this->count = $count;
	}
	/**
	 * 显示分页
	 * 
	 * @return string
	 */
	public function returnPage($url) {
		if(!isset($url)){
			$url = "?".$this->page_par."=";
		}
		if ($this->count==0){
			return '';
		}
		if ($this->whichPage == 1) {
			$sy = '<span><a href="javascript:;">首页</a></span>';
			$syy = ' <span><a href="javascript:;">上一页</a></span>';
		} else {
			
			$sy = '<a href="' . $url . '1">首页</a>';
			$syy = ' <a  href="' . $url . ($this->whichPage - 1) . '" >上一页</a>';
		}
		if ($this->whichPage == $this->countPage) {
			$wy = ' <span><a href="javascript:;">尾页</a></span>';
			$xyy = ' <span><a href="javascript:;">下一页</a></span>';
		} else {
			$wy = ' <a  href="' . $url . $this->countPage . '"  >尾页</a>';
			$xyy = ' <a  href="' . $url . ($this->whichPage + 1) . '">下一页</a>';
		}
		$cs = " 页次:" . $this->whichPage . "/" . $this->countPage . "　" . $this->count . "条记录 " . $this->several . "条/每页　转到";
		$i = 0;
		$ss = "";
		
		$start_page = $this->whichPage-10;
		$end_page = $this->whichPage+10;
		if($start_page<1){
			$start_page=1;
		}
		if($end_page>=$this->countPage){
			$end_page = $this->countPage;
		}
		
		for($i=$start_page;$i<=$end_page;$i++){
			$xz = '';
			if ($i == $this->whichPage) {
				$xz = "selected";
			}
			$ss = "<option value='" . $url . $i . "' " . $xz . " >第" . $i . "页</option>" . $ss;
		}
		$ss = '<script type="text/javascript">
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location=\'"+selObj.options[selObj.selectedIndex].value+"\'");
  if (restore) selObj.selectedIndex=0;
}
</script>  <select  onChange="MM_jumpMenu(\'window\',this,0)">' . $ss . ' </select> ';
		return $sy . $syy . $xyy . $wy . $cs . $ss;
	}
	
	/**
	 * ajax分页
	 * 
	 * @return string
	 */
	public function returnAjaxPage($event) {
		if ($this->whichPage == 1) {
			$sy = '<span>首页</span>';
			$syy = ' <span>上一页</span>';
		} else {
			$sy = '<a  onclick="' . $event . '(\'1\')">首页</a>';
			$syy = ' <a   onclick="' . $event . '(\'' . ($this->whichPage - 1) . '\')">上一页</a>';
		}
		if ($this->whichPage == $this->countPage) {
			$wy = ' <span>尾页</span>';
			$xyy = ' <span>下一页</span>';
		} else {
			$wy = ' <a   onclick="' . $event . '(\'' . $this->countPage . '\')" >尾页</a>';
			$xyy = ' <a   onclick="' . $event . '(\'' . ($this->whichPage + 1) . '\')">下一页</a>';
		}
		$cs = " 页次:" . $this->whichPage . "/" . $this->countPage . "　" . $this->count . "条记录 " . $this->several . "条/每页　转到";
		$i = 0;
		while ( $i < $this->countPage ) {
			$i ++;
			$xz = '';
			if ($i == $this->whichPage) {
				$xz = "selected";
			}
			$ss = "<option value='" . $i . "' " . $xz . " >第" . $i . "页</option>" . $ss;
		}
		$ss = "<select  onChange='" . $event . "(this.value)'>" . $ss . "</select>";
		return $sy . $syy . $xyy . $wy . $cs . $ss;
	}
}